<template>
  <pro-layout
    :menus="menus"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :i18nRender="i18nRender"
    v-bind="settings"
    style="margin: 0 !important;"
  >
    <!-- 系统logo + 名称 -->
    <template v-slot:menuHeaderRender>
      <div >
        <logo-svg />
        <h1>{{ title }}</h1>
      </div>
    </template>
    
    <!-- 配置设置 -->
    <setting-drawer :settings="settings" @change="handleSettingChange"/>

    <template v-slot:rightContentRender >
      <div style="float: right; width: auto;">
        <right-content :top-menu="settings.layout === 'topmenu'" :is-mobile="isMobile"  :theme="settings.theme" />
      </div>
    </template>

    <template v-slot:headerContentRender >
      <div style="float: right; width: auto;">
        <left-content :top-menu="settings.layout === 'topmenu'" :is-mobile="isMobile"  :theme="settings.theme" />
      </div>
    </template>

    <!-- 测试自创--废弃不用--仅供测试 -->
    <!-- <HistoryNav style="border: 1px solid #fff; height: 37px;"></HistoryNav> -->

    <!-- 配套的tabs组件（使用此组件需要配置相应的参数信息，需要注意  ---  tips by hexunjian at 2021-02-01） -->
    <MultiTab style="position: relative; top: 5px; margin: 0; padding-top: 5px;"></MultiTab>

    <!-- 主体内容区域 -->
    <router-view />

  </pro-layout>
</template>

<script>
import { SettingDrawer, updateTheme } from '@ant-design-vue/pro-layout'
// import { SettingDrawer } from '@/components/SettingDrawer/index'
// import { updateTheme } from '@/components/SettingDrawer/settingConfig'

import { i18nRender } from '@/locales'
import { mapState } from 'vuex'
import { CONTENT_WIDTH_TYPE, SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'

// 整体风格样式的配置都在此处了
import defaultSettings from '@/config/defaultSettings'

// 右侧主体
import RightContent from '@/components/GlobalHeader/RightContent'
import LeftContent from '@/components/GlobalHeader/LeftContent'
import GlobalFooter from '@/components/GlobalFooter'

// 引入历史导航
import HistoryNav from '@/components/drugCommon/HistoryNav'
import  MultiTab from '@/components/MultiTab/MultiTab'

export default {
  name: 'BasicLayout',
  components: {
    SettingDrawer,
    RightContent,
    GlobalFooter,
    HistoryNav,
    LeftContent,
    MultiTab
  },
  data () {
    return {
      // preview.pro.antdv.com only use.
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      // end

      multiTab: defaultSettings.multiTab,
      fixedHeader: defaultSettings.fixedHeader, // sticky header

      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      title: defaultSettings.title,
      settings: {
        // 布局类型
        layout: defaultSettings.layout,     // 'sidemenu', 'topmenu'
        // CONTENT_WIDTH_TYPE
        contentWidth: defaultSettings.layout === 'sidemenu' ? CONTENT_WIDTH_TYPE.Fluid : defaultSettings.contentWidth,
        // 主题 'dark' | 'light'
        theme: defaultSettings.navTheme,
        // 主色调
        primaryColor: defaultSettings.primaryColor,
        // primaryColor: "#ffffff",

        fixedHeader: defaultSettings.fixedHeader,
        fixSiderbar: defaultSettings.fixSiderbar,
        colorWeak: defaultSettings.colorWeak,
        hideHintAlert: false,
        // 隐藏 copy 按钮
        hideCopyButton: true,
      },
      // 媒体查询
      query: {},

      // 是否手机模式
      isMobile: false
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.addRouters
    })
  },
  created () {
    const routes = this.mainMenu.find(item => item.path === '/')
    this.menus = (routes && routes.children) || []
    // 处理侧栏收起状态
    this.$watch('collapsed', () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    })
    this.$watch('isMobile', () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    })
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }

    // first update color
    // TIPS: THEME COLOR HANDLER!! PLEASE CHECK THAT!!
    if (process.env.NODE_ENV !== 'production' || process.env.VUE_APP_PREVIEW === 'true') {
      updateTheme(this.settings.primaryColor)
    }
  },
  methods: {
    i18nRender,
    handleMediaQuery (val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.collapsed = false
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
        // this.settings.fixSiderbar = false
      }
    },
    handleCollapse (val) {
      this.collapsed = val
    },
    handleSettingChange ({ type, value }) {
      
      // 在此处增加展示模式的存储处理
      let tempParam = {}
      tempParam[type] = value
      // TO-DO-20201231: 发起请求把配置信息给后台

      type && (this.settings[type] = value)
      switch (type) {
        case 'contentWidth':
          this.settings[type] = value
          break
        case 'layout':
          if (value === 'sidemenu') {
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
          } else {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed
          }
          break
      }
    }
  }
}
</script>

<style lang="less">
@import "./BasicLayout.less";
</style>
